package com.android.gufengmall.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;

import androidx.annotation.RequiresApi;

import java.io.ByteArrayOutputStream;
import android.util.Base64;

import static android.util.Base64.DEFAULT;

public class BitMapConversionUtils {

    /**
     *  将Bitmap转字符串
     * @param bitmap bitmap图片
     * @return base64 字符串
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String bitmapToString(Bitmap bitmap) {
        String str;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,0,byteArrayOutputStream);
        byte[] bytes = byteArrayOutputStream.toByteArray();
        str = Base64.encodeToString(bytes, DEFAULT);
        return str;
    }

    /**
     *  将字符串转Bitmap
     * @param string 字符串
     * @return
     */
    public static Bitmap stringToBitmap(String string) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeFile(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}
